package com.ikcai.controller;

import com.ikcai.redis.util.JedisClusterFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import redis.clients.jedis.JedisCluster;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

/**
 * Created by daxiao on 2016/12/14.
 */
@Controller
public class PrintController {

    @Autowired
    private JedisClusterFactory jedisClusterFactory;

    @RequestMapping(value = "/print", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public void print(HttpServletRequest request, HttpServletResponse response, @RequestBody String requestBody, OutputStream out) {
        System.out.println(requestBody);

        JedisCluster JedisCluster;
        try {
            JedisCluster = jedisClusterFactory.getObject();
            JedisCluster.append("name", "zhangsan");
            JedisCluster.append("age", "18");
            JedisCluster.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String jsonStr = "{name:\"张三\",age:14}";
        try {

            out.write(jsonStr.getBytes("UTF-8"));
            out.flush();
            out.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
